package cn.jobstart.spider.downloader;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.PlainText;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 使用Selenium调用浏览器进行渲染。目前仅支持chrome。<br>
 * 需要下载Selenium driver支持。<br>
 */


public class SeleniumDownloader implements Downloader, Closeable {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    public WebDriver webDriver;
    private int sleepTime = 0;

    /**
     * 新建
     *
     * @param chromeDriverPath chromeDriverPath
     */
    public SeleniumDownloader(String chromeDriverPath) {
        System.getProperties().setProperty("webdriver.chrome.driver",
                chromeDriverPath);
    }

    public WebDriver getWebDriver() {
        return webDriver;
    }

    public SeleniumDownloader setWebDriver(WebDriver webDriver) {
        this.webDriver = webDriver;
        return this;
    }

    /**
     * 设置搜索条件
     */
    public void settingsSearchCondition() {


    }

    /**
     * set sleep time to wait until load success
     *
     * @param sleepTime sleepTime
     * @return this
     */
    public SeleniumDownloader setSleepTime(int sleepTime) {
        this.sleepTime = sleepTime;
        return this;
    }


    @Override
    public Page download(Request request, Task task) {

        logger.info("downloading page " + request.getUrl());
        webDriver.get(request.getUrl());
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        settingsSearchCondition();




       Map<String,String> headers= task.getSite().getHeaders();

       headers.forEach((k,v)->{

           System.out.println(k+":"+v);
       });



        Page page = new Page();

      /*  List<WebElement> elements = webDriver.findElements(By.xpath("//*[@id=\"menuMainContainer\"]/section/div/div/div[1]/div[2]/div[2]//div[@role='listitem']"));

        for (WebElement element : elements) {
            element.click();
            setSleepTime(5000);
            WebElement webElement = webDriver.findElement(By.xpath("/html"));
            String content = webElement.getAttribute("outerHTML");

            page.setRawText(content);
            // page.setHtml(new Html(content, request.getUrl()));
            page.setUrl(new PlainText(request.getUrl()));
            page.setRequest(request);
            return page;

        }*/


        WebElement webElement = webDriver.findElement(By.xpath("/html"));
        String content = webElement.getAttribute("outerHTML");

        page.setRawText(content);
        // page.setHtml(new Html(content, request.getUrl()));
        page.setUrl(new PlainText(request.getUrl()));
        page.setRequest(request);

        return page;
    }

    /**
     * Tell the downloader how many threads the spider used.
     *
     * @param threadNum number of threads
     */
    @Override
    public void setThread(int threadNum) {

    }


    /**
     * Tell the downloader how many threads the spider used.
     *
     * @param threadNum number of threads
     */


    @Override
    public void close() throws IOException {
        // webDriver.close();
    }
}
